<#assign content>
<div class="right_col" role="main">
    <div class="">
        <div class="page-title">
            <div class="title_left">
                <h3>产品配方</h3>
            </div>
            <div class="title_right">
            </div>
        </div>
        <div class="clearfix"></div>
        <div class="row">
            <div class="col-md-12 col-sm-12 col-xs-12">
                <div class="x_panel">
                    <div class="x_title">
                        <h2>
                            <small></small>
                        </h2>
                        <ul class="nav navbar-right panel_toolbox">
                            <li><a id="materialAdd"><i class="fa fa-plus-circle"></i></a></li>
                            <li><a class="collapse-link"><i class="fa fa-chevron-up"></i></a></li>
                        </ul>
                        <div class="clearfix"></div>
                    </div>
                    <div class="x_content">
                        <#--产品信息显示-->
                        <div class="alert alert-info alert-dismissible fade in" role="alert">
                            <#if sProduct.price?exists>
                                <#assign price = MoneyTag.priceToString(sProduct.price) />
                            <#else>
                                <#assign price = 0 />
                            </#if>
                            <strong>产品名：${sProduct.name}  产品描述：${sProduct.description} 产品价格：${price}</strong>
                        </div>
                        <div style="display: none"></div>
                        <#--原料小块-->
                        <#if formulaList?exists && formulaList?size gt 0>
                        <#list formulaList as formulaList>
                            <div class="col-md-4 col-sm-4 col-xs-12 profile_details">
                                <div class="well profile_view" style="width: 100%">
                                    <div class="col-sm-12">
                                        <p><strong>原料名：${formulaList.materialname}</strong></p>
                                        <p><strong>原料单位：${formulaList.materialunit}</strong></p>
                                        <p><strong>消耗数量：${formulaList.amount}</strong></p>
                                    </div>
                                    <div class="col-xs-12 bottom text-center">
                                        <div class="col-xs-12 col-sm-12 emphasis">
                                            <button onclick="$.editFormula(${formulaList.id})" type="button" class="btn btn-success btn-xs"><i class="fa fa-pencil"></i>修改</button>
                                            <button onclick="$.deleteFormula(${formulaList.id})" type="button" class="btn btn-danger btn-xs"><i class="fa fa-trash-o"></i>删除</button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </#list>
                        </#if>
                    </div>
                    <div class="x_content">
                        <#-- 分割线 -->
                        <div style="border-top: 2px solid #E6E9ED; margin: 0px 1% 20px 1%;"></div>
                        <#-- 配方小块 -->
                        <#if formulbList?exists && formulbList?size gt 0>
                            <#list formulbList as formulbList>
                                <div class="col-md-4 col-sm-4 col-xs-12 profile_details">
                                    <div class="well profile_view" style="width: 100%">
                                        <div class="col-sm-12">
                                            <p><strong>配方名：${formulbList.materialname}</strong></p>
                                            <p><strong>配方单位：${formulbList.materialunit}</strong></p>
                                            <p><strong>消耗数量：${formulbList.amount}</strong></p>
                                        </div>
                                        <div class="col-xs-12 bottom text-center">
                                            <div class="col-xs-12 col-sm-12 emphasis">
                                                <button onclick="$.editFormula(${formulbList.id})" type="button" class="btn btn-success btn-xs"><i class="fa fa-pencil"></i>修改</button>
                                                <button onclick="$.deleteFormula(${formulbList.id})" type="button" class="btn btn-danger btn-xs"><i class="fa fa-trash-o"></i>删除</button>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </#list>
                        </#if>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</#assign>
<#include "../_inc/layout.ftl" />

<#--产品原料新增-->
<div class="modal fade" id="materialAddModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content" style="width: 700px;">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">产品原料新增</h4>
            </div>
            <form id="materialAddForm" method="post" novalidate class="form-horizontal form-label-left">
                <input type="hidden" id="add_productid" name="productid" value="<#if productid?exists>${productid}<#else>0</#if>">
                <div class="modal-body">
                    <div class="item form-group">
                        <label class="control-label col-md-3">产品原料<span class="required">*</span>
                        </label>
                        <div class="col-md-5">
                            <select name="meterialid" id="meterialid" class="form-control col-md-6" required>
                                <option value="">---请选择---</option>
                            <#if materialList?exists && materialList?size gt 0><#list materialList as materialList>
                                <option value="${materialList.id}">${materialList.name}(${materialList.unit})</option>
                            </#list></#if>
                            </select>
                        </div>
                    </div>
                </div>

                <div class="modal-body">
                    <div class="item form-group">
                        <label class="control-label col-md-3">数量<span class="required">*</span>
                        </label>
                        <div class="col-md-5">
                            <input type="text" id="addamount" name="addamount" data-validate-length-range="5,12"
                                   placeholder="请输入数量" required="required" class="form-control col-md-6">
                        </div>
                    </div>
                </div>
                <div class="ln_solid"></div>
                <div class="form-group">
                    <div class="col-md-8 col-md-offset-9">
                        <button id="saveMaterial" type="button" class="btn btn-success">保存</button>
                        <button class="btn btn-primary" type="reset">重置</button>
                    </div>
                </div>
            </form>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>

<#--产品原料修改-->
<div class="modal fade" id="formulaEditModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
     aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content" style="width: 700px;">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">产品原料修改</h4>
            </div>
            <form id="formulaEditForm" method="post" novalidate class="form-horizontal form-label-left">
                <input type="hidden" id="edit_productid" name="productid" value="<#if productid?exists>${productid}<#else>0</#if>">
                <input type="hidden" id="edit_formulaid" name="id">
                <div class="modal-body">
                    <div class="item form-group">
                        <label class="control-label col-md-3">原料<span class="required">*</span>
                        </label>
                        <div class="col-md-5" >
                                <label id="editmaterialname" name="editmaterialname" data-validate-length-range="5,12"
                                       placeholder="" required="required" class="control-label">
                                </label>
                        </div>
                    </div>
                </div>
                <div class="modal-body">
                    <div class="item form-group">
                        <label class="control-label col-md-3">数量<span class="required">*</span>
                        </label>
                        <div class="col-md-5">
                            <input type="text" id="editamount" name="editamount" data-validate-length-range="5,12"
                                   placeholder="请输入数量" required="required" class="form-control col-md-6">
                        </div>
                    </div>
                </div>
                <div class="ln_solid"></div>
                <div class="form-group">
                    <div class="col-md-8 col-md-offset-9">
                        <button id="saveEditFormula" type="button" class="btn btn-success">保存</button>
                        <button class="btn btn-primary" type="reset">重置</button>
                    </div>
                </div>
            </form>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>

<#--配方删除-->
<div class="modal fade" id="formulaDeleteModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
     aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content" style="width: 700px;">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">配方删除</h4>
            </div>
            <form id="formulaDeleteForm" method="post" novalidate class="form-horizontal form-label-left">
                <input type="hidden" id="delete_formulaid" name="id">
                <input type="hidden" id="delete_productid" name="productid" value="<#if productid?exists>${productid}<#else>0</#if>">
                <div class="modal-body">
                    <div class="item form-group">
                        <div class="col-md-10">
                            <span id="formulaDeleteMessage"></span>
                        </div>
                    </div>
                </div>
                <div class="ln_solid"></div>
                <div class="form-group">
                    <div class="col-md-8 col-md-offset-9">
                        <button id="deleteFormulaInfo" type="button" class="btn btn-success">确定</button>
                        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    </div>
                </div>
            </form>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>

<script>
    var formulatable;

    $(document).ready(function () {


        //产品配方新增
        $('#materialAdd').on('click', function () {
            $('#materialAddModal').modal({
                keyboard: true
            });
        });

        // 窗体关闭清空页面
        $('#basicsAdd').on('hide.bs.modal', function () {
            $('#basicsAddForm')[0].reset();
        });

        // 窗体关闭清空页面
        $('#basicsEditModal').on('hide.bs.modal', function () {
            $('#basicsEditForm')[0].reset();
        });


        // 提交保存配方
        $('#saveMaterial').on('click', function () {
            if (validator.checkAll($('#materialAddForm'))) {
                var param = {};
                param.action = 'do';
                param.productid = $('#add_productid').val();
                param.meterialid = $('#meterialid').val();
                param.amount = $('#addamount').val();
                $('#materialAddForm')[0].reset();
                $('#materialAddModal').modal('hide');
                $.ajax({
                    type: 'POST',
                    url: '/formula_add.do',
                    cache: false,
                    dataType: 'json',
                    data: param,
                    success: function (result) {
                        if (result.success) {
                            var params = {};
                            params.productid = $('#add_productid').val();
                            $.submitFormPost('/formula_list.do', params, 'POST');
                        }
                        else {
                            $.promptModal('${warning}', result.msg, result.logout);
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        $.promptModal('${warning}', '${systemException}', false);
                    }
                });
            }
        });



        // 修改产品原料
        $.editFormula = function (id) {
            var param = {};
            param.action = 'edit';
            param.id = id;
            $.ajax({
                type: 'POST',
                url: '/formula_edit.do',
                cache: false,
                dataType: 'json',
                data: param,
                success: function (result) {
                    if (result.success) {
                        $('#edit_formulaid').val(result.body.sFormula.id);
                        $('#editmaterialname').text(result.body.sMaterial.name);
                        $('#editamount').val(result.body.sFormula.amount);
                        $('#formulaEditModal').modal({
                            keyboard: true
                        });
                    }
                    else {
                        $.promptModal('${warning}', result.msg, result.logout);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    $.promptModal('${warning}', '${systemException}', false);
                }
            });
        };

        // 修改产品原料
        $.editBatics = function (id) {
            var param = {};
            param.action = 'edit';
            param.id = id;
            $.ajax({
                type: 'POST',
                url: '/formula_edit.do',
                cache: false,
                dataType: 'json',
                data: param,
                success: function (result) {
                    if (result.success) {
                        $('#edit_formulaid').val(result.body.sFormula.id);
                        $('#editmaterialname').text(result.body.sMaterial.name);
                        $('#editamount').val(result.body.sFormula.amount);
                        $('#formulaEditModal').modal({
                            keyboard: true
                        });
                    }
                    else {
                        $.promptModal('${warning}', result.msg, result.logout);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    $.promptModal('${warning}', '${systemException}', false);
                }
            });
        };

        // 保存修改产品原料
        $('#saveEditFormula').on('click', function () {
            if (validator.checkAll($('#formulaEditForm'))) {
                var param = {};
                param.action = 'doEdit';
                param.id = $('#edit_formulaid').val();
                param.amount = $('#editamount').val();
                param.pid = $('#edit_productid').val();
                $('#formulaEditForm')[0].reset();
                $('#formulaEditModal').modal('hide');
                $.ajax({
                    type: 'POST',
                    url: '/formula_edit.do',
                    cache: false,
                    dataType: 'json',
                    data: param,
                    success: function (result) {
                        if (result.success) {
                            var params = {};
                            params.productid = $('#edit_productid').val();
                            $.submitFormPost('/formula_list.do', params, 'POST');
                        }
                        else {
                            $.promptModal('${warning}', result.msg, result.logout);
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        $.promptModal('${warning}', '${systemException}', false);
                    }
                });
            }
        });

        // 删除
        $.deleteFormula = function (id) {
            $('#delete_formulaid').val(id);
            $('#formulaDeleteMessage').html('确定删除配方信息？');
            $('#formulaDeleteModal').modal({
                keyboard: true
            });
        };

        // 保存删除
        $('#deleteFormulaInfo').on('click', function () {
            var param = {};
            param.action = 'doDelete';
            param.id = $('#delete_formulaid').val();
            $('#formulaDeleteModal').modal('hide');
            $.ajax({
                type: 'POST',
                url: '/formula_edit.do',
                cache: false,
                dataType: 'json',
                data: param,
                success: function (result) {
                    if (result.success) {
                        var params = {};
                        params.productid = $('#delete_productid').val();
                        $.submitFormPost('/formula_list.do', params, 'POST');
                    }
                    else {
                        $.promptModal('${warning}', result.msg, result.logout);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    $.promptModal('${warning}', '${systemException}', false);
                }
            });
        });
    });
</script>

